package fr.mywiki.business.entry.meta.datatype;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Set;

import fr.mywiki.business.resources.ResourceManager;
import fr.mywiki.business.utils.StringUtils;

/** The linked objects are entries. */
public class EntryLink extends Choice {
  public static final String TYPE_NAME = "entryLink";
  
  public static final String TYPE_BUNDLE_KEY = "model.type.entryLink.display";
  public static final String TYPE_BUNDLE_KEY_MORE = "model.type.entryLink.display.morethan";
  public static final String TYPE_BUNDLE_KEY_LESS = "model.type.entryLink.display.lessthan";
  public static final String TYPE_BUNDLE_KEY_BETWEEN = "model.type.entryLink.display.between";
  public static final String TYPE_BUNDLE_KEY_AMONG = "model.type.entryLink.display.among";
  
  /* The errors */
  /** Problem of type */
  public static final String INVALID_TYPE = "model.validation.entrylink.badtype";
  public static final String INVALID_NUMBER_MIN = "model.validation.entrylink.lessmin";
  public static final String INVALID_NUMBER_MAX = "model.validation.entrylink.moremax";
  public static final String INVALID_OBJECT = "model.validation.entrylink.badelement";
  
  /**
   * Format : "Choice [of n+/- entries] [among {...}];
   */
  public String describe(Locale locale) {
  	StringBuffer buffer = new StringBuffer();
		
		buffer.append(ResourceManager.getMessage(TYPE_BUNDLE_KEY, locale));
		
		if (minLinks > MIN_LINKS) {
			if (maxLinks > MAX_LINKS) {
				// Between ...
				buffer.append(" "+ResourceManager.getMessage(TYPE_BUNDLE_KEY_BETWEEN,
						locale, new String[] { Integer.toString(minLinks),
								Integer.toString(maxLinks) }));
			} else {
				// More than ...
				buffer.append(" "+ResourceManager.getMessage(TYPE_BUNDLE_KEY_MORE, locale,
						new String[] { Integer.toString(minLinks) }));
			}
		} else if(maxLinks>MAX_LINKS) {
			// Less than ...
			buffer.append(" "+ResourceManager.getMessage(TYPE_BUNDLE_KEY_LESS, locale,
					new String[] { Integer.toString(maxLinks) }));
		}
		
		if (potentials != null)
			buffer.append(" "+ResourceManager.getMessage(TYPE_BUNDLE_KEY_AMONG, locale,
					new String[] { StringUtils.toString(new ArrayList(potentials)) }));
		
		return buffer.toString();
	}
  
  public EntryLink() {
    this(MIN_LINKS, MAX_LINKS, null);
  }

  public EntryLink(int minLinks, int maxLinks) {
    this(minLinks, maxLinks, null);
  }

  public EntryLink(Set potentials) {
    this(MIN_LINKS, MAX_LINKS, potentials);
  }

  public EntryLink(int minLinks, int maxLinks, Set potentials) {
    setMinLinks(minLinks);
    setMaxLinks(maxLinks);
    setPotentials(potentials);
  }
  
  public String getDisplayKey() {
  	return TYPE_BUNDLE_KEY;
  }
  
  public String getTypeName() {
    return TYPE_NAME;
  }
}
